let uid = 0
export default class Watcher {
    constructor(vm, expOrFn, cb, options, isRenderWatcher) {
        this.vm = vm
        if (isRenderWatcher) {
            vm._watcher = this
        }
        vm._watchers.push(this)
        this.deep = this.user = this.lazy = this.sync = false
        this.cb = cb
        this.id = ++uid // uid for batching
        this.getter = expOrFn
    }

    update() {

        this.run()

    }

    get() {
        let value
        const vm = this.vm
        value = this.getter.call(vm, vm)
        this.cleanupDeps()
        return value
    }

    addDep(dep) {
        dep.addSub(this)
    }

    cleanupDeps() {
        let i = this.deps.length
        while (i--) {
            const dep = this.deps[i]
            if (!this.newDepIds.has(dep.id)) {
                dep.removeSub(this)
            }
        }
        let tmp = this.depIds
        this.depIds = this.newDepIds
        this.newDepIds = tmp
        this.newDepIds.clear()
        tmp = this.deps
        this.deps = this.newDeps
        this.newDeps = tmp
        this.newDeps.length = 0
    }

    run() {
        const value = this.get()
        console.log(value)
        const oldValue = this.value
        this.cb.call(this.vm, value, oldValue)
    }
}